<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/img/logo.svg" alt="logo" />
      <span class="title" v-if="!props.isCollapse">Vue3+TS</span>
    </div>
    <el-menu
      class="el-menu-vertical"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
      :collapse="props.isCollapse"
      :default-active="activeMenu"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!-- 二级菜单 -->
        <template v-if="item.type === 1">
          <!-- 二级菜单可以展开的标题 -->
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- <i v-if="item.icon" :class="item.icon"></i> -->
              <template v-if="item.icon">
                <el-icon :class="item.icon" v-if="item.icon === 'el-icon-monitor'">
                  <Monitor />
                </el-icon>
                <el-icon :class="item.icon" v-else-if="item.icon === 'el-icon-setting'">
                  <Setting />
                </el-icon>
                <el-icon
                  :class="item.icon"
                  v-else-if="item.icon === 'el-icon-chat-line-round'"
                >
                  <ChatLineRound />
                </el-icon>
                <el-icon :class="item.icon" v-else-if="item.icon === 'el-icon-goods'">
                  <Goods />
                </el-icon>
              </template>
              <span>{{ item.name }}</span>
            </template>
            <!-- 遍历里面的item -->
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item :index="subitem.id + ''" @click="handleMenuClick(subitem)">
                <i v-if="subitem.icon" :class="subitem.icon"></i>
                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 一级菜单 -->
        <!-- <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <i v-if="item.icon" :class="item.icon"></i>
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template> -->
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { defineProps, computed, ref } from 'vue'
import { Setting, Monitor, Goods, ChatLineRound } from '@element-plus/icons-vue'
import { useStore } from '@/store'
import { useRouter, useRoute } from 'vue-router'
import { pathMapToMenu } from '@/utils/map-menu'

const props = defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})

const store = useStore()
const router = useRouter()
const route = useRoute()

const userMenus = computed(() => {
  return store.state.login.userMenus
})
// 当前活跃的菜单
const currentPath = route.path
const menu = pathMapToMenu(userMenus.value, currentPath)
const activeMenu = ref(menu.id + '')
// console.log(currentPath)
// console.log(activeMenu.value)

const handleMenuClick = (item: any) => {
  router.push({ path: item.url ?? 'not-found' })
}
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;
  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    .img {
      height: 100%;
      margin: 0 10px;
    }
    .title {
      font-size: 16px;
      font-weight: 700;
      color: #fff;
    }
  }
}
.el-menu {
  border-right: none;
}
.el-menu-item {
  height: 44px;
}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
.el-submenu {
  background-color: #001529 !important;
  // 二级菜单 ( 默认背景 )
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135 !important;
  }
}
::v-deep .el-submenu__title {
  background-color: #001529 !important;
}

// hover 高亮
.el-menu-item:hover {
  color: #fff !important; // 菜单
}

.el-menu-item.is-active {
  color: #fff !important;
  background-color: #0a60bd !important;
}
</style>
